package com.atjava.webspider.gui.site;

import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.atjava.webspider.gui.table.DefaultTable;
import com.atjava.webspider.gui.table.DefaultTableModel;
import com.atjava.webspider.logic.ConfigLogic;
import com.atjava.webspider.model.Site;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.util.DialogUtil;
import com.atjava.webspider.util.TableUtil;

public class SiteManageGui extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	
	private DefaultTableModel siteModel;
	private JPanel jPanelSite = null;
	private JScrollPane jScrollPaneSite = null;
	private JTable jTableSite = null;
	private JButton jButtonNew = null;
	private JButton jButtonView = null;
	private JButton jButtonDel = null;

	
	public SiteManageGui(Frame owner,boolean modal) {
		super(owner,modal);
		initialize();
		setTextValue();
	}
	
	private void setTextValue(){
		try {
			List<Site> sites = ConfigLogic.getSiteConfig().getSites();
			if(sites != null){
				DefaultTableModel dtm = (DefaultTableModel)jTableSite.getModel();
				TableService serv = TableService.getInstance();
				dtm.updateAllData(serv.getSiteTable(sites));
				if(dtm.getRowCount() > 0)
					jTableSite.setRowSelectionInterval(0, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			DialogUtil.showError(this, "获取站点列表失败！");
		}
	}
	
	public DefaultTableModel getSiteModel(){
		return siteModel;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(600, 350);
		this.setResizable(false);
		this.setTitle("站点管理");
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo(getOwner());
		this.setModal(true);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJPanelSite(), null);
			jContentPane.add(getJButtonNew(), null);
			jContentPane.add(getJButtonView(), null);
			jContentPane.add(getJButtonDel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTableSite	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableSite() {
		if (jTableSite == null) {
			String [] columns = new String []{"站点名称","字符集","页面","替换"};
			siteModel = new DefaultTableModel(columns);
			siteModel.setEditable(true);
			jTableSite = new DefaultTable(siteModel);
			//调整大小
			TableUtil.fitTableColumns(jTableSite,new int[]{200,50,50,50});
			jTableSite.setRowHeight(25);
			TableUtil.setTableAlign(jTableSite, JLabel.CENTER);
		}
		return jTableSite;
	}

	/**
	 * This method initializes jPanelSite	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelSite() {
		if (jPanelSite == null) {
			jPanelSite = new JPanel();
			jPanelSite.setLayout(null);
			jPanelSite.setBounds(new Rectangle(1, 0, 592, 285));
			jPanelSite.setBorder(BorderFactory.createTitledBorder(null, " 站点列表 "));
			jPanelSite.add(getJScrollPaneSite(), null);
		}
		return jPanelSite;
	}

	/**
	 * This method initializes jScrollPaneSite	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneSite() {
		if (jScrollPaneSite == null) {
			jScrollPaneSite = new JScrollPane();
			jScrollPaneSite.setBounds(new Rectangle(8, 19, 576, 257));
			jScrollPaneSite.setFont(new Font("Courier New", Font.PLAIN, 12));
			jScrollPaneSite.setViewportView(getJTableSite());
		}
		return jScrollPaneSite;
	}

	/**
	 * This method initializes jButtonNew	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNew() {
		if (jButtonNew == null) {
			jButtonNew = new JButton();
			jButtonNew.setBounds(new Rectangle(300, 289, 90, 25));
			jButtonNew.setText("新建站点");
			jButtonNew.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					NewSiteGui newSite = new NewSiteGui(SiteManageGui.this,true);
					newSite.setVisible(true);
				}
			});
		}
		return jButtonNew;
	}

	/**
	 * This method initializes jButtonView	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonView() {
		if (jButtonView == null) {
			jButtonView = new JButton();
			jButtonView.setBounds(new Rectangle(400, 289, 90, 25));
			jButtonView.setText("查看站点");
			jButtonView.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int rows = siteModel.getRowCount();
					int n = jTableSite.getSelectedRow();
					List<Site> sites = ConfigLogic.getSiteConfig().getSites();
					if(rows > 0 && n >= 0){
						NewSiteGui newOutput = new NewSiteGui
						(SiteManageGui.this,true,sites.get(n));
						newOutput.setVisible(true);
					}
				}
			});
		}
		return jButtonView;
	}

	/**
	 * This method initializes jButtonDel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonDel() {
		if (jButtonDel == null) {
			jButtonDel = new JButton();
			jButtonDel.setBounds(new Rectangle(500, 289, 90, 25));
			jButtonDel.setText("删除站点");
			jButtonDel.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int rows = siteModel.getRowCount();
					int n = jTableSite.getSelectedRow();
					if(rows > 0 && n >= 0){
						List<Site> sites = ConfigLogic.getSiteConfig().getSites();
						if(DialogUtil.confirmMessage(SiteManageGui.this, "真的要删除 [ " +sites.get(n).getName() + " ] 这个站点的全部信息吗？")){
							sites.remove(n);
							siteModel.removeData(n);
						}
					}
					//保存全局信息
					ConfigLogic.saveSiteConfig();
				}
			});
		}
		return jButtonDel;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
